package org.ftc.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.CTMap;
import org.ftc.domain.Committee;
import org.ftc.domain.CommitteeTeam;
import org.ftc.form.CommitteeTeamForm;
import org.ftc.service.CTMapService;
import org.ftc.service.CommitteeTeamService;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.ftc.util.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CommitteeTeamController {
	@Autowired
	UsersService usersService;
	@Autowired
	CommitteeTeamService committeeTeamService;
	@Autowired
	CTMapService ctMapService;
	@Autowired
	FtcLogService logService;

	@RequestMapping(value = "/committeeTeam.htm")
	public String codeCause(ModelMap model) {
		return "committeeTeam/list";
	}

	@RequestMapping(value = "/committeeTeam/list.json", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> find(ModelMap model) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		List<CommitteeTeam> data = committeeTeamService.findAll();
		json.put("rows", data);
		logService.save(Log.QUERY, this.getClass().toString());
		return json;
	}

	@RequestMapping(value = "/committeeTeam/update.json", method = RequestMethod.POST)
	public @ResponseBody Object save(CommitteeTeam entity, BindingResult binding, ModelMap model) {
		logService.before();
		if (entity == null || entity.getDate() == null) {
			return true;
		}

		if (StringUtils.isBlank(entity.gettCode())) {
			committeeTeamService.save(entity);
			logService.save(Log.INSERT, entity.gettCode(), this.getClass().toString(),
					ToStringBuilder.reflectionToString(entity));
		} else {
			CommitteeTeam committeeTeam = committeeTeamService.get(entity.gettCode());
			committeeTeam.setDate(entity.getDate());
			committeeTeam.setEnable(entity.isEnable());
			committeeTeamService.update(committeeTeam);
			logService.save(Log.UPDATE, entity.gettCode(), this.getClass().toString(),
					ToStringBuilder.reflectionToString(entity));
		}
		return entity;
	}

	@RequestMapping(value = "/committeeTeam/{committeeSeq}/update.json", method = RequestMethod.GET)
	public @ResponseBody boolean updateStatus(@PathVariable String committeeSeq) {
		logService.before();
		boolean result = false;
		CommitteeTeam checked = committeeTeamService.getChecked();
		if (checked != null) {
			checked.setEnable(false);
			result = committeeTeamService.update(checked);
			if (!result) {
				return result;
			}
		}

		CommitteeTeam committeeTeam = committeeTeamService.get(committeeSeq);
		committeeTeam.setEnable(true);
		result = committeeTeamService.update(committeeTeam);
		logService.save(Log.UPDATE, committeeSeq, this.getClass().toString(), committeeSeq);
		return result;
	}

	@RequestMapping(value = "/committeeTeam/{committeeSeq}/updateGroup.json", method = RequestMethod.POST)
	public @ResponseBody boolean updateGroup(@PathVariable String committeeSeq, CommitteeTeamForm cForm, ModelMap model) {
		logService.before();
		boolean result = false;
		if (StringUtils.isBlank(committeeSeq) || StringUtils.isBlank(cForm.getName())) {
			return result;
		}
		Set<CTMap> set = new HashSet<CTMap>();
		CommitteeTeam committeeTeam = committeeTeamService.get(committeeSeq);

		String[] committees = StringUtils.split(cForm.getName(), ",");
		CTMap ctMap = null;
		Committee committee = null;
		for (String id : committees) {
			ctMap = new CTMap();
			Authentication userDetails = SecurityContextHolder.getContext().getAuthentication();
			if (userDetails != null) {
				ctMap.setCreationUser(userDetails.getName());
			}
			ctMap.setCreationDate(new Date());
			ctMap.setCommitteeTeam(committeeTeam);
			committee = new Committee();
			committee.setCode(id);
			ctMap.setCommittee(committee);
			set.add(ctMap);
		}
		committeeTeam.setEmMapping(set);
		result = committeeTeamService.update(committeeTeam);
		logService.save(Log.UPDATE, committeeSeq, this.getClass().toString(), committeeSeq);
		return result;
	}

	@RequestMapping(value = "/committeeTeam/delete/{committeeSeq}.json")
	public @ResponseBody boolean delete(@PathVariable String committeeSeq) {
		logService.before();
		Boolean s = false;
		if (StringUtils.isBlank(committeeSeq)) {
			return s;
		}
		s = committeeTeamService.delete(committeeSeq);
		logService.save(Log.DELETE, committeeSeq, this.getClass().toString());
		return s;
	}

	@RequestMapping(value = "/committeeTeam/findAll.json", method = RequestMethod.GET)
	public @ResponseBody List<CommitteeTeam> findAll() {
		return committeeTeamService.findAll();
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(Global.APP.DATE_FORMAT);
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

}
